﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_NewsStat : System.Web.UI.Page
{
    protected int CurrentPage;
    enum Sorting { ByTitle, ByDate,Views };
    
    NewsType pageType;
    Sorting currentSorting;
    protected string sPageTitle;
    protected void Page_Load(object sender, EventArgs e)
    {
        GetParameters();

        if (!IsPostBack)
        {
            currentSorting = Sorting.ByDate;
            ViewState["Sorting"] = currentSorting;
            GetData();
            txtKeywords.Text = PageHeaderData.GetPageKeywordsByType(pageType);
        }
        else
        {
            currentSorting = (Sorting)ViewState["Sorting"];
        }

        switch(pageType)
        {
            case NewsType.news:
                sPageTitle = "Новости";
                break;
            case NewsType.popular:
                sPageTitle = "Популярные статьи";
                break;
            case NewsType.MedicalTourism:
                sPageTitle = "Медецинский туризм";
                gvNews.Columns[2].Visible = false;
            break;
        }
        lnkTopNews.NavigateUrl = "topNews.aspx?type=" + (int)pageType;
        
    }

    protected void gvNews_PageindexChanging(object sender, GridViewPageEventArgs e)
    {
        CurrentPage = e.NewPageIndex + 1;
        gvNews.PageIndex = e.NewPageIndex;
        GetData();
    }

    private void GetParameters()
    {
        pageType = (NewsType)Convert.ToInt32(Request.QueryString["type"]);
        if (pageType > 0)
            ViewState["pageType"] = pageType;
    }


    protected void gvNews_Sorting(object sender, GridViewSortEventArgs e)
    {
        switch (e.SortExpression)
        {
            case "Title":
                currentSorting = Sorting.ByTitle;
                break;
            case "Date":
                currentSorting = Sorting.ByDate;
                break;
            case "Views":
                currentSorting = Sorting.Views;
                break;
        }
        ViewState["Sorting"] = currentSorting;
        GetData();
    }

    private void GetData()
    {
        ObjectDataSource ods = new ObjectDataSource();
        ods.ID = "ods" + 1;
        ods.EnablePaging = gvNews.AllowPaging; //the paging of ODS depends on the paging of the GridView
        ods.TypeName = "MyTableAdapter"; //be sure to prefix the namespace of your application !!! e.g. MyApp.MyTableAdapter

        ods.SelectMethod = "GetData";
        ods.SelectCountMethod = "VirtualItemCount";
        ods.StartRowIndexParameterName = "startRow";
        ods.MaximumRowsParameterName = "maxRows";
        ods.EnableViewState = false;

        ods.ObjectCreating += new ObjectDataSourceObjectEventHandler(ods_ObjectCreating);
        gvNews.DataSource = ods;
        gvNews.DataBind();

        //if (ddlPageNumber.Items.Count == 0 || GVTrivias.PageCount != ddlPageNumber.Items.Count)
        //{
        //    ddlPageNumber.Items.Clear();
        // for (int icount = 1; icount < gvNews.PageCount + 1; icount++)
        //   {
        ////        ListItem li = new ListItem(icount.ToString(), icount.ToString());
        ////        ddlPageNumber.Items.Add(li);
        //    }
        //    lblShowRecords.Text = string.Format(" {0} ", GVTrivias.PageCount);
        //}

    }

    private void ods_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        //calling a "parametered" constructor of my own TableAdapter and passing it to the ObjectInstance property 
        Dal.arParams.Clear();
        Dal.arParams.Add((int)pageType);//CatID
        Dal.arParams.Add(CurrentPage);//Current Page
        Dal.arParams.Add(gvNews.PageSize);//PageSize
        Dal.arParams.Add((int)currentSorting);//PageSize
        DataSet DS = Dal.ExecuteDBspDS("Admin_GetNewsByCategoryWPaging", Dal.arParams);

        if (DS != null)
        {
            if (DS.Tables.Count > 0)
            {
                MyTableAdapter tAdapter = new MyTableAdapter(DS.Tables[0]);
                e.ObjectInstance = tAdapter;
            }
            //and that'all there's to it !!
        }
    }

    protected void chkPopular_Changed(Object sender, EventArgs e)
    {
        Int32 iLinkID;
        string _val = ((CheckBox)sender).Attributes["CommandArgument"];
        Int32.TryParse(_val, out iLinkID);
        
        Dal.arParams.Clear();
        Dal.arParams.Add(iLinkID);//PageSize
        Dal.arParams.Add(((CheckBox)sender).Checked);//Checked
        Dal.ExecuteDBsp("Admin_SetPopular", Dal.arParams);

        GetData();
    }

    protected void chkVisible_Changed(Object sender, EventArgs e)
    {
        Int32 iLinkID;
        string _val = ((CheckBox)sender).Attributes["CommandArgument"];
        Int32.TryParse(_val, out iLinkID);

        Dal.arParams.Clear();
        Dal.arParams.Add(iLinkID);//PageSize
        Dal.arParams.Add(((CheckBox)sender).Checked);//Checked
        Dal.ExecuteDBsp("Admin_SetVisible", Dal.arParams);

        GetData();
    }

    protected void chkTop_Changed(Object sender, EventArgs e)
    {
        Int32 iLinkID;
        string _val = ((CheckBox)sender).Attributes["CommandArgument"];
        Int32.TryParse(_val, out iLinkID);

        Dal.arParams.Clear();
        Dal.arParams.Add((int)pageType);//Checked
        Dal.arParams.Add(iLinkID);//PageSize
        Dal.ExecuteDBsp("Admin_SetTopNews", Dal.arParams);

        GetData();
    }

    protected void btnDeleteNews_Click(Object sender, CommandEventArgs e)
    {
        Int32 iLinkID;
        Int32.TryParse(e.CommandArgument.ToString(), out iLinkID);

        Dal.arParams.Clear();
        Dal.arParams.Add(iLinkID);
        Dal.ExecuteDBsp("Admin_DeleteArticle", Dal.arParams);
        GetData();
    }

    protected void btnEditNews_Click(Object sender, CommandEventArgs e)
    {
        Int32 iLinkID;

        Int32.TryParse(e.CommandArgument.ToString(), out iLinkID);

        Response.Redirect("Addarticle.aspx?id=" + iLinkID);
    }

    protected void btnAddKeywords_Click(Object sender, EventArgs e)
    {
        PageHeaderData.SetPageKeywords(pageType, txtKeywords.Text);
    }

    // ConfirmDelete
    protected void gvNews_DataBinding(object sender, GridViewRowEventArgs e)
    {
        AjaxControlToolkit.ConfirmButtonExtender cb = new AjaxControlToolkit.ConfirmButtonExtender();
        try
        {
            cb.TargetControlID = e.Row.FindControl("btnDeleteNews").ClientID.Replace("_", "$");
            cb.OnClientCancel = "CancelClick";
            cb.ConfirmText = "Вы уверены , что хотите удалить запись ?";
            e.Row.Cells[6].Controls.Add(cb);
        }
        catch { }
    }
}
